package com.crape.account.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.crape.account.bo.IAccountBO;
import com.crape.account.bo.IAuthEmailBO;
import com.crape.common.enums.ResultType;
import com.crape.common.model.Account;
import com.crape.common.model.AccountInfo;
import com.crape.common.tools.DESEncoder;
import com.crape.common.tools.IDESEncoder;
import com.crape.common.utils.RegisterCheck;

@Controller
public class MissPasswordController {

    private IAccountBO       accountBOImpl;
    private IAuthEmailBO     authEmailBOImpl;

    @Autowired
    public void setAccountBOImpl(IAccountBO accountBOImpl) {
        this.accountBOImpl = accountBOImpl;
    }

    @Autowired
    public void setAuthEmailBOImpl(IAuthEmailBO authEmailBOImpl) {
        this.authEmailBOImpl = authEmailBOImpl;
    }

    @RequestMapping("/missPassword/validateEmail.json")
    public ModelAndView validateEmail(String email) {
        ModelAndView mv = new ModelAndView();
        try {
            boolean isEmail = RegisterCheck.isEmail(email);
            if (!isEmail) {
                mv.addObject("flag", ResultType.FAILURE.getCode());
                mv.addObject("message", "邮箱格式有问题，请修改。");
                return mv;
            }
            Account account = accountBOImpl.getAccountByEmail(email);
            if (account == null) {
                mv.addObject("message", "该邮箱不存在。");
                mv.addObject("flag", ResultType.FAILURE.getCode());
                return mv;
            }
            mv.addObject("flag", ResultType.SUCCESS.getCode());
            return mv;
        } catch (Exception e) {
            mv.addObject("message", e.getMessage());
            mv.addObject("flag", ResultType.FAILURE.getCode());
            return mv;
        }
    }

    @RequestMapping("/missPassword/sendEmail.jsp")
    public String sendEmail(HttpServletRequest request, ModelMap modelMap, String email, String checkcode) {
        String sessionCode = (String) request.getSession().getAttribute("validateCode");
        if (null == sessionCode) {
            modelMap.put("message", "会话已过期，请重新输入!");
            return "/alert.html";
        }
        if (!checkcode.toUpperCase().equalsIgnoreCase(sessionCode)) {
            modelMap.put("message", "验证码不正确！");
            return "/alert.html";
        }
        Account account = accountBOImpl.getAccountByEmail(email);
        if (account == null) {
            modelMap.put("message", "该邮箱不存在。");
            return "/alert.html";
        }
        authEmailBOImpl.missPassword(account);
        modelMap.put("message", "重置密码邮件已发送！请查收。");
        modelMap.put("email", email);
        return "/auth/sendPwSuccess.html";
    }

    @RequestMapping("/missPassword/reSendEmail.jsp")
    public String reSendEmail(ModelMap modelMap, String email) {
        Account account = accountBOImpl.getAccountByEmail(email);
        if (account == null) {
            modelMap.put("message", "该邮箱不存在。");
            return "/alert.html";
        }
        authEmailBOImpl.missPassword(account);
        modelMap.put("message", "重置密码邮件已发送！请查收。");
        return "/auth/sendPwSuccess.html";
    }

    @RequestMapping("/auth/changePassword.jsp")
    public String changePassword(ModelMap modelMap, String username,String uuid) {
        if (username == null || username.isEmpty()||uuid == null || uuid.isEmpty()) {
            modelMap.put("message", "访问链接无效！");
            return "/alert.html";
        }
        AccountInfo account = new AccountInfo();
        account.setUsername(username);
        account.setUuid(uuid);
        List<AccountInfo> acctlist = accountBOImpl.getAccountInfos(account);
        if (acctlist == null || acctlist.isEmpty()) {
            modelMap.put("message", "访问链接已过期！");
            return "/alert.html";
        }
        AccountInfo acct = acctlist.get(0);
        modelMap.put("account", acct);
        return "/auth/changePassword.html";
    }

    @RequestMapping("/auth/resetPassword.jsp")
    public String resetPassword(ModelMap modelMap, Account account) {
        AccountInfo acct = accountBOImpl.getAccountInfoByUsername(account.getUsername());
        if (acct == null) {
            modelMap.put("message", "用户不存在！");
            return "/alert.html";
        }
        if (account.getNewpassword() == null || account.getNewpassword().isEmpty()) {
            modelMap.put("message", "请输入新密码");
            return "/alert.html";
        }
        if (account.getConfirm() == null || account.getConfirm().isEmpty()) {
            modelMap.put("message", "请输入确认密码");
            return "/alert.html";
        }
        if (!account.getConfirm().equals(account.getNewpassword())) {
            modelMap.put("message", "确认密码不一致");
            return "/alert.html";
        }
        IDESEncoder encoder = DESEncoder.getInstance();
        accountBOImpl.updatePassword(acct.getAccountid(), encoder.Encode(account.getNewpassword()));
        modelMap.put("message", "密码修改成功！");
        return "/auth/resetPwSuccess.html";
    }

}
